<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
</head>
<body style="background:#eef">
<?
	/*
	// Преобразование индекса категори в его группу.
	$categories = array(
		1 => 10,
		2 => 46,
		3 => 13,
		4 => 27,
		5 => 72,
		6 => 28,
		7 => 28,
		8 => 49,
		9 => 50,
		10 => 51,
		11 => 52,
		12 => 53,
		13 => 15,
		14 => 16,
		15 => 17,
		16 => 18,
		17 => 19,
		18 => 0,
		19 => 32,
		20 => 33,
		21 => 69,
		22 => 20,
		23 => 21,
		24 => 22,
		25 => 23,
		26 => 24,
		27 => 29,
		28 => 66,
		29 => 67,
		30 => 75,
		31 => 68,
		32 => 0,
		33 => 76,
		34 => 34,
		35 => 69,
		36 => 36,
		37 => 63,
		38 => 64,
		39 => 11,
		40 => 30,
		41 => 72,
		42 => 43,
		43 => 37,
		44 => 36,
		45 => 44,
		46 => 56,
		47 => 57,
		48 => 58,
		49 => 78,
		50 => 37,
		51 => 38,
		52 => 59,
		53 => 25,
		54 => 39,
		55 => 40,
		56 => 79,
		57 => 70,
		58 => 71,
		59 => 73,
		60 => 80,
		61 => 0,
		62 => 0,
		63 => 52,
		64 => 0,
		65 => 61,
		66 => 12,
		67 => 47,
		68 => 41,
		69 => 0,
		70 => 60,
		71 => 26
	);
	*/
	
	//$categories = array();
	// Добиваем до 100 категорий нулями.
	//for ($i = 1; $i < 101; $i++)
	//	$categories[$i] = 0;
	
	// Сохранить список категорий.
	function save_categories()
	{
		global $categories;
		//print_r($categories);
		$f = fopen('categories.php', 'w+');
		fwrite($f, '<?php /*'.serialize($categories).'*/?>');
		fclose($f);
	}
	
	// Загрузить список категорий.
	function load_categories()
	{
		global $categories;
		$f = fopen('categories.php', 'r');
		fread($f, 8);
		$categories = fread($f, filesize('categories.php') - 12);
		fclose($f);
		$categories = unserialize($categories);
	}
	
	load_categories();
	//print_r($categories);
	
	// Запрос на сохранение категорий.
	if (isset($_REQUEST['cmd']) && ($_REQUEST['cmd'] == 'save') )
	{
		// Проверка пароля.
		$pass = isset($_REQUEST['pass']) ? $_REQUEST['pass'] : '';
		if ($pass == 'anex_pass')
		{
			// Получаем данные.
			for($i = 1; $i < 101; $i++)
			{
				$categories[$i] = (int)$_REQUEST['item_'.$i];
			}
			// Сохраняем категории.
			save_categories();
			echo '<font color="green">Данные сохранены.</font><br>';
		}
		else
			echo '<font color="red">Пароль ошибочен.</font><br>';
	}	

	// Подключаем конфиг БД.
	include('configuration.php');
	$conf = new JConfig();
	
	// Подключаемся к БД.
	mysql_connect($conf->host, $conf->user, $conf->password);
	mysql_select_db($conf->db);
	mysql_query("SET NAMES utf8");
	mysql_query("SET CHARACTER SET utf8"); 
	
	// Выбираем существующие категории сайта anex.
	$r = mysql_query('SELECT category_id, category_name './/, xref1.category_child_id as xref1_category_child_id, xref1.category_parent_id as xref1_category_parent_id, xref2.* 
		//' FROM jos_vm_category cat');//, jos_vm_category_xref'
		' FROM jos_vm_category cat, jos_vm_category_xref xref1, jos_vm_category_xref xref2 '.
		//' WHERE category_child_id=category_id AND category_parent_id=0');
		' WHERE xref1.category_child_id=category_id AND ((xref2.category_child_id=xref1.category_parent_id AND xref2.category_parent_id=0) OR (0=xref1.category_parent_id))'.
		' ORDER BY category_name'
		);
	$site_categories = array();
	while($a = mysql_fetch_assoc($r))
	{
		//echo '<pre>';
		//print_r($a);
		$site_categories[$a['category_id']] = $a['category_name'];
	}
	
	// Выводим таблицу соответствия категорий сайта netlab и anex.
	echo '<form action="grabber_setup.php" method="post">';
	echo '<input type="hidden" name="cmd" value="save" />';
	echo 'Пароль<input type="text" name="pass" />';
	echo '<input type="submit" value="Сохранить данные">';
	echo '<table cellpadding="0" cellspacing="8">';
	//ksort($categories, SORT_NUMERIC);
	foreach($categories as $index => $category_id)
	{
		echo '<tr style="background:#ccc">';
		echo '<td align="right" style="border:1px outset #bbb">'.$index;//.'</td>';
		//echo '<td>';
		echo '<select name="item_'.$index.'" style="border:0">';
		echo '<option value="0" ></option>';
		
		// Список категорий сайта для выбора.
		foreach($site_categories as $cat_id => $cat_name)
		{
			$selected = ($category_id == $cat_id) ? 'selected' : '';
			echo '<option value="'.$cat_id.'" '.$selected.'>'.$cat_name.'</option>';
		}
		
		echo '</select>';
		echo '</td>';
		echo '</tr>';
	}
	
	echo '</table>';
	echo '</form>'
?>
</body>
</html>